package cn.kgc.springboot1048.spring_vue2.controller;


import cn.kgc.springboot1048.spring_vue2.entity.Houseinfo;
import cn.kgc.springboot1048.spring_vue2.service.HouseinfoService;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author szz
 * @since 2024-04-10
 */
@CrossOrigin
@RestController
@RequestMapping("/spring_vue2/houseinfo")
public class HouseinfoController {

    @Resource
    HouseinfoService houseinfoService;

    @GetMapping(value = "list")
    public R selectAll(@RequestParam(value = "keyword",required = false,defaultValue = "")String desc,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                       @RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex
                       ){
        PageInfo<Houseinfo> pageInfo = houseinfoService.selectAll(desc, pageSize, pageIndex);
        return R.ok(pageInfo).setCode(20000);
    }


    @GetMapping(value = "/del/{id}")
    public R del(@PathVariable("id") Integer id){

        return R.ok(houseinfoService.del(id));
    }

    @PostMapping(value = "/add")
    public R add(@RequestBody Houseinfo houseinfo){
        houseinfo.setPublishDate(new Date());
        int i = houseinfoService.add(houseinfo);
        return R.ok(i);
    }

    @GetMapping(value = "/get/{id}")
    public R get(@PathVariable("id") Integer id){
        return R.ok(houseinfoService.selectById(id));
    }

    @PostMapping(value = "/update")
    public R update(@RequestBody Houseinfo houseinfo){
        return R.ok(houseinfoService.updateByHouse(houseinfo));
    }

    @GetMapping(value = "/selectDesc")
    public R selectByDesc(@RequestParam(value = "keyword",required = false,defaultValue = "") String keyword){
        List<Houseinfo> houseinfos = houseinfoService.selectByDesc(keyword);
        return  R.ok(houseinfos);
    }


}

